package jp.ddo.trismegistos.dao.impl;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.ddo.trismegistos.bean.Entry;
import jp.ddo.trismegistos.dao.DaoManager;
import jp.ddo.trismegistos.dao.EntryDao;
import jp.ddo.trismegistos.util.TimestampUtil;

public class EntryDaoImpl extends BaseDao implements EntryDao {

	public EntryDaoImpl(DaoManager manager) {
		super(manager);
	}

	public List<Entry> findEntryByUserId(final String userId) {
		final Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		return executeSelect("findEntryByUserId", params, Entry.class);
	}

	public int addEntry(final String userId, final String title,
			final String text) {
		final Timestamp now = TimestampUtil.getTimestampNow();
		final Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("title", title);
		params.put("text", text);
		params.put("now", now);
		return executeInsert("addEntry", params);
	}

	@Override
	public Entry findEntryByEntryId(String entryId) {
		final Map<String, Object> params = new HashMap<String, Object>();
		params.put("entryId", entryId);
		final List<Entry> entries = executeSelect("findEntryByEntryId", params,
				Entry.class);
		// エントリIDから取得するのでListの中身は必ず1個のはず
		return entries.get(0);
	}
}
